package com.yuhanginfo.modules.web.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.WebCrudService;
import com.yuhanginfo.common.utils.ReadProperties;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.dao.GoodsListDao;
import com.yuhanginfo.modules.web.dao.WebCarDao;
import com.yuhanginfo.modules.web.dao.WebClassesDao;
import com.yuhanginfo.modules.web.entity.WebCar;
import com.yuhanginfo.modules.web.entity.WebClasses;
import com.yuhanginfo.modules.web.entity.WebGoods;

@Service
@Transactional(readOnly = true)
public class GoodsListService extends WebCrudService<GoodsListDao, WebGoods> {

	@Autowired
	private GoodsListDao goodsListDao;
	@Autowired
	private WebCarDao carDao;
	@Autowired
	private WebClassesDao webClassDao;

	public List<WebGoods> getGoodsListByStoreCode(Map<String,Object> params) {
		if(StringUtils.isNotEmpty((String)params.get("classId"))) {
			WebClasses webClass=webClassDao.get(params.get("classId").toString());
			String parentids=webClass.getParentIds();
			if(StringUtils.isNotBlank(parentids)) {
				parentids=parentids.substring(0,parentids.length()-1);
				String[] arr=parentids.split(",");
				if(arr.length>1) {//2个parentid
					params.put("firstClassId", arr[0]);
					params.put("secondClassId", arr[1]);
					params.put("thirdClassId", webClass.getId());
				}else {
					params.put("firstClassId", arr[0]);
					params.put("secondClassId", webClass.getId());
				}
			}else {
				params.put("firstClassId", webClass.getId());
			}
		}
		List<WebGoods> goodsList = goodsListDao.getGoodsList(params);
		List<WebGoods> resultList = new ArrayList<WebGoods>();
		for (WebGoods webGoods : goodsList) {
			if (webGoods.getStoreStock() != null && webGoods.getStoreStock() > 0) {
				webGoods.setCarList(getCarInfoByPartNo(webGoods.getPartCode()));
				resultList.add(webGoods);
			}
		}
		for (WebGoods webGoods : goodsList) {
			if (webGoods.getStoreStock() == null || webGoods.getStoreStock() == 0) {
				webGoods.setCarList(getCarInfoByPartNo(webGoods.getPartCode()));
				resultList.add(webGoods);
			}
		}
		return resultList;
	}
	/**
	 * 分页用
	 * author lvls
	 */
	@SuppressWarnings("unchecked")
	public Page<WebGoods> getGoodsListByStoreCode(Page<WebGoods> page, WebGoods WebGoods,Map<String,Object> params) {
		if(StringUtils.isNotEmpty((String)params.get("classId"))) {
			WebClasses webClass=webClassDao.get(params.get("classId").toString());
			String parentids=webClass.getParentIds();
			if(StringUtils.isNotBlank(parentids)) {
				parentids=parentids.substring(0,parentids.length()-1);
				String[] arr=parentids.split(",");
				if(arr.length>1) {//2个parentid
					params.put("firstClassId", arr[0]);
					params.put("secondClassId", arr[1]);
					params.put("thirdClassId", webClass.getId());
				}else {
					params.put("firstClassId", arr[0]);
					params.put("secondClassId", webClass.getId());
				}
			}else {
				params.put("firstClassId", webClass.getId());
			}
		}
		String pageSize = ReadProperties.ReadByName("page.webPageSize");
		page.setPageSize(Integer.parseInt(pageSize));
		WebGoods.setPage(page);
		//将参数赋值给webgoods对象
		WebGoods.setFirstClassId((String)params.get("firstClassId"));
		WebGoods.setSecondClassId((String)params.get("secondClassId"));
		WebGoods.setThirdClassId((String)params.get("thirdClassId"));
		WebGoods.setCarBarand((String)params.get("carBarand"));
		WebGoods.setVenderType((String)params.get("venderType"));
		WebGoods.setIsgoodsNum((String)params.get("isgoodsNum"));
		WebGoods.setCarCode((String)params.get("carCode"));
		WebGoods.setCarYear((String)params.get("carYear"));
		WebGoods.setIds((List<String>) params.get("ids"));
		WebGoods.setSearchContent((String) params.get("searchContent"));
		WebGoods.setStoreType((String) params.get("storeType"));
		List<WebGoods> goodsList = goodsListDao.getGoodsPage(WebGoods);
		List<WebGoods> resultList = new ArrayList<WebGoods>();
		for (WebGoods webGoods : goodsList) {
			if (webGoods.getStoreStock() != null && webGoods.getStoreStock() > 0) {
				webGoods.setCarList(getCarInfoByPartNo(webGoods.getPartCode()));
				resultList.add(webGoods);
			}
		}
		for (WebGoods webGoods : goodsList) {
			if (webGoods.getStoreStock() == null || webGoods.getStoreStock() == 0) {
				webGoods.setCarList(getCarInfoByPartNo(webGoods.getPartCode()));
				resultList.add(webGoods);
			}
		}
		page.setList(resultList);
		return page;
	}
	
	


	public List<String> getCarInfoByPartNo(String partNo) {
		List<String> resultList = new ArrayList<String>();
		List<WebCar> carList = carDao.getCarInfoByPartNo(partNo);
		for (WebCar webCar : carList) {
			String carBarand=StringUtils.isNotEmpty(webCar.getCarBarand())?webCar.getCarBarand():"";
			String carCode=StringUtils.isNotEmpty(webCar.getCarCode())?webCar.getCarCode():"";
			String carYear=StringUtils.isNotEmpty(webCar.getCarYear())?webCar.getCarYear():"";
			String carInfo = carBarand + "/" + carCode + "/" + carYear;
			resultList.add(carInfo);
		}
		return resultList;
	}

	public List<WebCar> getAllCarBarand() {
		List<WebCar> carList = (List<WebCar>)WebUserUtils.getCache("allCarBarand");
		if(carList==null) {
			carList=carDao.getAllCarBarand();
			WebUserUtils.putCache("allCarBarand",carList);
		}
		return carList;
	}
	
	
	public List<String> getCarCodeByBarand(String barandCode){
		String carCodeCache="carCode_"+barandCode;
		List<String> carCodeList=(List<String>)WebUserUtils.getCache(carCodeCache);
		if(carCodeList==null) {
			carCodeList=carDao.getCarCodeByBarand(barandCode);
			WebUserUtils.putCache(carCodeCache,carCodeList);
		}
		return carCodeList;
	}
	
	public List<String> getCarYearByCode(String code){
		String carYearCache="carYear_"+code;
		List<String> carYearList=(List<String>)WebUserUtils.getCache(carYearCache);
		if(carYearList==null) {
			carYearList=carDao.getCarYearByCode(code);
			WebUserUtils.putCache(carYearCache,carYearList);
		}
		return carYearList;
	}
	
	public String getAllClasses(String id) {
		String result="";
		WebClasses webClasses=webClassDao.get(id);
		String parentids=webClasses.getParentIds();
		if(StringUtils.isNotBlank(parentids)) {
			parentids=parentids.substring(0,parentids.length()-1);
			String[] arr=parentids.split(",");
			List<String> idsList = Arrays.asList(arr);
			List<WebClasses> classList= webClassDao.findAllClassesByParentId(idsList);
			for(WebClasses webClass:classList) {
				result+=webClass.getName()+"/";
			}
			result+=webClasses.getName();
		}else {
			result=webClasses.getName();
		}
		return result;
	}
	
	public WebGoods getGoodsByCode(Map<String,String> params) {
		WebGoods webGoods=goodsListDao.getGoodsByCode(params);
		String partCode=webGoods.getPartCode();
		List<String> carInfo=getCarInfoByPartNo(partCode);
		webGoods.setCarList(carInfo);
		return webGoods;
	}
	
	public WebGoods goodsComboToList(Map<String,String> params) {
		WebGoods webGoods=goodsListDao.goodsComboToList(params);
		if(webGoods!=null){
			String partCode=webGoods.getPartCode();
			List<String> carInfo=getCarInfoByPartNo(partCode);
			webGoods.setCarList(carInfo);
		}
		return webGoods;
	}
	
	
	public List<WebCar> getCarTypeAndFirstCode(String barandCode){
		return carDao.getCarTypeAndFirstCode(barandCode);
	}
	
	public List<WebCar> getCarType(Map<String, String> map){
		return carDao.getCarType(map);
	}
	
	public List<WebCar> getCarCode(String carType){
		return carDao.getCarCode(carType);
	}
	
	public List<WebCar> getCarYear(String carCode){
		return carDao.getCarYear(carCode);
	}
	
	public List<WebCar> getCarInfo(){
		List<WebCar> carList = (List<WebCar>)WebUserUtils.getCache("fiveLvCar");//品牌-大写-类型-车型-年
		if(carList==null) {
			carList=getAllCarBarand();
			for(WebCar carBarand:carList) {
				List<WebCar> firstCodeList=getCarTypeAndFirstCode(carBarand.getCarBarandCode());
				List<WebCar> typeChNotNullList = new ArrayList<WebCar>();
				for (WebCar webCar : firstCodeList) {
					if(!"".equals(webCar.getCarTypeCh())){
						typeChNotNullList.add(webCar);
					}
				}
				carBarand.setChildList(typeChNotNullList);
				for(WebCar firstCode:typeChNotNullList) {
					Map<String, String> map=new HashMap<String, String>();
					map.put("firstCode", firstCode.getCarTypeCh());
					map.put("barandCode", carBarand.getCarBarandCode());
					List<WebCar> carTypeList=getCarType(map);
					for(WebCar c:carTypeList) {
						c.setCarTypeUUID(UUID.randomUUID().toString().replaceAll("-", ""));
					}
					firstCode.setChildList(carTypeList);
					for(WebCar carType:carTypeList) {
						List<WebCar> carCodeList=getCarCode(carType.getCarType());
						for(WebCar c:carCodeList) {
							c.setCarCodeUUID(UUID.randomUUID().toString().replaceAll("-", ""));
						}
						carType.setChildList(carCodeList);
						for(WebCar carCode:carCodeList) {
							List<WebCar> carYearList=getCarYear(carCode.getCarCode());
							carCode.setChildList(carYearList);
						}
					}
				}
			}
			WebUserUtils.putCache("fiveLvCar", carList);
		}
		return carList;
	}
	
}
